package org.example.curriculum_design_6_16.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.example.curriculum_design_6_16.entity.pojo.SocialService;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;
import java.util.Map;

/**
 * 社会服务Mapper接口
 * @author 郑凯
 * @description 针对表【social_service】的数据库操作Mapper
 * @createDate 2025-06-16 09:26:01
 * @Entity org.example.curriculum_design_6_16.entity.pojo.SocialService
 */
@Mapper
public interface SocialServiceMapper extends BaseMapper<SocialService> {
    
    /**
     * 根据学生ID和学年查询社会服务记录
     */
    @Select("SELECT * FROM social_service WHERE student_id = #{studentId} AND academic_year = #{academicYear} ORDER BY created_at DESC")
    List<SocialService> findByStudentIdAndYear(@Param("studentId") String studentId,
                                               @Param("academicYear") String academicYear);

    /**
     * 根据学生ID查询社会服务记录
     */
    @Select("SELECT ss.*, ssr.rule_name, ssr.score_value, ssr.description " +
            "FROM social_service ss " +
            "JOIN social_service_rule ssr ON ss.service_type = ssr.rule_key " +
            "WHERE ss.student_id = #{studentId} AND ssr.is_active = 1 " +
            "ORDER BY ss.created_at DESC")
    List<SocialService> findByStudentId(@Param("studentId") String studentId);

    /**
     * 根据学生ID、服务类型和学年查询记录
     */
    @Select("SELECT * FROM social_service WHERE student_id = #{studentId} AND service_type = #{serviceType} AND academic_year = #{academicYear}")
    List<SocialService> findByStudentIdAndTypeAndYear(
            @Param("studentId") String studentId,
            @Param("serviceType") String serviceType,
            @Param("academicYear") String academicYear);

    /**
     * 统计规则8之前的记录数
     */
    @Select("SELECT COUNT(*) FROM social_service " +
            "WHERE student_id = #{studentId} " +
            "AND service_type IN ('volunteer_service', 'inspection_practice') " +
            "AND academic_year < #{academicYear}")
    int countRule8BeforeYear(@Param("studentId") String studentId,
                             @Param("academicYear") String academicYear);

    /**
     * 更新记录分数
     */
    @Update("UPDATE social_service SET score = #{score} WHERE id = #{id}")
    int updateScore(@Param("id") Integer id, @Param("score") Double score);
    
    /**
     * 更新记录状态
     */
    @Update("UPDATE social_service SET status = #{status}, review_comment = #{comment}, updated_at = NOW() WHERE id = #{id}")
    int updateStatus(@Param("id") Integer id, @Param("status") Integer status, @Param("comment") String comment);
    
    /**
     * 获取待审核的记录
     */
    @Select("SELECT * FROM social_service WHERE status = 0 ORDER BY created_at ASC")
    List<SocialService> findPendingRecords();
    
    /**
     * 获取已通过审核的记录
     */
    @Select("SELECT * FROM social_service WHERE student_id = #{studentId} AND academic_year = #{academicYear} AND status = 1")
    List<SocialService> findApprovedRecords(@Param("studentId") String studentId, @Param("academicYear") String academicYear);
    
    /**
     * 统计学生社会服务记录数量
     */
    @Select("SELECT service_type, COUNT(*) as count, SUM(score) as total_score " +
            "FROM social_service " +
            "WHERE student_id = #{studentId} AND academic_year = #{academicYear} AND status = 1 " +
            "GROUP BY service_type")
    List<Map<String, Object>> getStudentStatistics(@Param("studentId") String studentId, @Param("academicYear") String academicYear);
    
    /**
     * 获取学生社会服务总分
     */
    @Select("SELECT COALESCE(SUM(score), 0) as total_score " +
            "FROM social_service " +
            "WHERE student_id = #{studentId} AND academic_year = #{academicYear} AND status = 1")
    Double getStudentTotalScore(@Param("studentId") String studentId, @Param("academicYear") String academicYear);
    
    /**
     * 检查学生是否已达到某规则的最大次数限制
     */
    @Select("SELECT COUNT(*) FROM social_service " +
            "WHERE student_id = #{studentId} AND service_type = #{serviceType} AND status = 1")
    int countStudentServiceType(@Param("studentId") String studentId, @Param("serviceType") String serviceType);
    
    /**
     * 批量更新学生分数
     */
    @Update("UPDATE social_service SET score = #{score}, updated_at = NOW() WHERE id = #{id}")
    int batchUpdateScore(@Param("id") Integer id, @Param("score") Double score);

    @Select("SELECT COUNT(*) FROM social_service " +
            "WHERE student_id = #{studentId} " +
            "AND academic_year = #{academicYear} " +
            "AND is_approved = 0" +
            "AND rule_type = activity")
    int countActivity(
            @Param("studentId") String studentId,
            @Param("academicYear") String academicYear);
}




